<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Config;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="附近列表")
 */
class NearbyList extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Position();
        $this->connect_todatabase();
    }

 
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
           $admin = session("admin");
            if($admin["type"] == 1){
                $count = Db::connect('tmp')->table('job_position')
                    ->where($where)
                    ->where("deleted_at",NULL)
                    ->count();
                $list = Db::connect('tmp')->table('job_position')
                    ->where($where)
                    ->where("deleted_at",NULL)
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();
                foreach($list as $k=>$v){
                    //所属分类 category_name
                    $job_position_category = Db::connect('tmp')->table('job_position_category')->where("job_position_id",$v["id"])->select()->toArray();
                    $job_position_category = array_column($job_position_category,"category_id");
                    //dump($job_position_category);die;

                    //公司名称
                    $company = Db::connect('tmp')->table('company')->where("id",$v["company_id"])->find();
                   
                    if(!empty($company["company_name"])){
                        $v["company_name"] = $company["company_name"];
                    }else{
                        $v["company_name"] = $company["contact_person_name"];
                    }
                    //招聘区域
                    
                    $region_province = Db::connect('tmp')->table('area')->where("code",$v["region_province"])->find();
                    $region_city = Db::connect('tmp')->table('area')->where("code",$v["region_city"])->find();
                    $region_region = Db::connect('tmp')->table('area')->where("code",$v["region_region"])->find();
                    if(!empty($region_province)){
                        $region_province = $region_province["name"];
                    }else{
                        $region_province = "";
                    }
                    if(!empty($region_city)){
                        $region_city = $region_city["name"];
                    }else{
                        $region_city = "";
                    }
                    if(!empty($region_region)){
                        $region_region = $region_region["name"];
                    }else{
                        $region_region = "";
                    }
                    $recruitment_area = $region_province.$region_city.$region_region;
                    if(!empty($recruitment_area)){
                        $v["recruitment_area"] = $recruitment_area;
                    }else{
                        $v["recruitment_area"] = "全国";
                    }
                    
                
                    //浏览量和浏览人数
                    $job_position_statistics = Db::connect('tmp')->table('job_position_statistics')->where("job_position_id",$v["id"])->find();
                
                    if(!empty($job_position_statistics)){
                        $v["pv"] = $job_position_statistics["pv"];
                        $v["uv"] = $job_position_statistics["uv"];
                    }else{
                        $v["pv"] = 0;
                        $v["uv"] = 0;
                    }
                    $v["admintype"]  = $admin["type"];
                    $list[$k] = $v;
                }
            }else{
                $count = Db::connect('tmp')->table('job_position')
                    ->where($where)
                    ->where("deleted_at",NULL)
                    ->where("company_id",$admin["company_id"])
                    ->count();
                $list = Db::connect('tmp')->table('job_position')
                    ->where($where)
                    ->where("deleted_at",NULL)
                    ->where("company_id",$admin["company_id"])
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();
                foreach($list as $k=>$v){
                    //公司名称
                    $company = Db::connect('tmp')->table('company')->where("id",$v["company_id"])->find();
                    if(!empty($company["company_name"])){
                        $v["company_name"] = $company["company_name"];
                    }else{
                        $v["company_name"] = $company["contact_person_name"];
                    }
                    //招聘区域

 
                    $region_province = Db::connect('tmp')->table('area')->where("code",$v["region_province"])->find();
                    if(!empty($region_province)){
                        $region_province = $region_province["name"];
                    }else{
                        $region_province = "";
                    }

                    if(!empty($v["region_city"])){
                        $region_city = Db::connect('tmp')->table('area')->where("code",$v["region_city"])->find();
                        if(!empty($region_city)){
                            $region_city = $region_city["name"];
                        }else{
                            $region_city = "";
                        }
                    }else{
                        $region_city = "";
                    }

                    if(!empty($v["region_region"])){
                        $region_region = Db::connect('tmp')->table('area')->where("code",$v["region_region"])->find();
                        if(!empty($region_region)){
                            $region_region = $region_region["name"];
                        }else{
                            $region_region = "";
                        }
                    }else{
                        $region_region = "";
                    }
                    $recruitment_area = $region_province.$region_city.$region_region;
                    if(!empty($recruitment_area)){
                        $v["recruitment_area"] = $recruitment_area;
                    }else{
                        $v["recruitment_area"] = "全国";
                    }
                    
                
                    //浏览量和浏览人数
                    $job_position_statistics = Db::connect('tmp')->table('job_position_statistics')->where("job_position_id",$v["id"])->find();
                
                    if(!empty($job_position_statistics)){
                        $v["pv"] = $job_position_statistics["pv"];
                        $v["uv"] = $job_position_statistics["uv"];
                    }else{
                        $v["pv"] = 0;
                        $v["uv"] = 0;
                    }
                    $v["admintype"]  = $admin["type"];
                    $list[$k] = $v;
                }
            }
            
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        $company_id = session('admin.company_id');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            if($post["contact_type"] == 1){
                if(empty($post["phone_number"])){
                    $this->error("手机号不能为空");
                }
                if(empty($post["recruit_num"])){
                    $this->error("最大数量不能为空");
                }
            }elseif($post["contact_type"] == 2){
                if(empty($post["qq_number"]) && empty($post["qq_qr_code"])){
                    $this->error("QQ号和QQ二维码不能同时为空");
                }
                if(empty($post["qqrecruit_num"])){
                    $this->error("最大数量不能为空");
                }
            }elseif($post["contact_type"] == 3){
                if(empty($post["wechat_number"]) && empty($post["wechat_qr_code"])){
                    $this->error("微信号和微信二维码不能同时为空");
                }
                if(empty($post["recruit_num"])){
                    $this->error("最大数量不能为空");
                }
            }else{
                $this->error("类型不存在");
            }
            if(empty($company_id)){
                if(empty($post["category_id"])){
                    $this->error("分类不能为空");
                }
                if(empty($post["tags"])){
                    $this->error("职位标签不能为空");
                }
                $post["tags"] = explode(",",$post["tags"]);
                $tags = json_encode($post["tags"]);
            }
           
            if($post["isit_nationwide"] == 1){
                if(empty($post["region_province"])){
                    $this->error("省不能为空");
                }
                if(empty($post["region_city"])){
                    $this->error("市不能为空");
                }
                if(empty($post["region_region"])){
                    $this->error("区不能为空");
                }
            }
         
           if(empty($post["selection_date_start"])){
                $selection_date_start = NULL;
           }
            if(empty($post["selection_date_end"])){
                $selection_date_end = NULL;
            }
            if(empty($post["work_time_start"])){
                $work_time_start = NULL;
            }
            if(empty($post["work_time_end"])){
                $work_time_end = NULL;
            }
            if(empty($post["expiration_date"])){
                $expiration_date = NULL;
            }
         
            if(empty($company_id)){
                $data = [
                    "company_id"=>$post["company_id"],
                    "position_name"=>$post["position_name"],
                    "salary"=>$post["salary"],
                    "salary_type"=>$post["salary_type"],
                    "region_province"=>$post["region_province"],
                    "region_city"=>$post["region_city"],
                    "region_region"=>$post["region_region"],
                    "job_description"=>$post["job_description"],
                    "selection_date_start"=>$selection_date_start,
                    "selection_date_end"=>$selection_date_end,
                    "work_time_start"=>$work_time_start,
                    "work_time_end"=>$work_time_end,
                    "greeting_message"=>$post["greeting_message"],
                    "contact_type"=>$post["contact_type"],
                    "gender_requirement"=>$post["gender_requirement"],
                    "work_experience"=>$post["work_experience"],
                    "min_age"=>$post["min_age"],
                    "max_age"=>$post["max_age"],
                    "experience_requirement"=>$post["experience_requirement"],
                    "cur_type"=>$post["cur_type"],
                    "expiration_date"=>$expiration_date,
                    "expected_num"=>$post["expected_num"],
                   
                    "tags"=>$tags,
                    "label"=>$post["label"],
                    "title_tag"=>$post["title_tag"],
                    "created_at"=>date("Y-m-d H:i:s",time()),
                    "cur_status"=>1
                ];
            }else{
                $data = [
                    "company_id"=>$company_id,
                    "position_name"=>$post["position_name"],
                    "salary"=>$post["salary"],
                    "salary_type"=>$post["salary_type"],
                    "region_province"=>$post["region_province"],
                    "region_city"=>$post["region_city"],
                    "region_region"=>$post["region_region"],
                    "job_description"=>$post["job_description"],
                    "selection_date_start"=>$selection_date_start,
                    "selection_date_end"=>$selection_date_end,
                    "work_time_start"=>$work_time_start,
                    "work_time_end"=>$work_time_end,
                    "contact_message"=>$post["contact_message"],
                    "greeting_message"=>$post["greeting_message"],
                    "contact_type"=>$post["contact_type"],
                    "created_at"=>date("Y-m-d H:i:s",time()),
                    "cur_status"=>3
                ];
            }

            
 
            try {
                Db::connect('tmp')->table('job_position')->insert($data);
                if(empty($company_id)){
                    $job_position_id = Db::connect('tmp')->table('job_position')->getLastInsID();
                }else{
                    $job_position_id = $company_id;
                }
            
                 //判断联系类型 1手机 2QQ 3微信
                if($post["contact_type"] == 1){
                    $data_phone = [
                        "job_position_id"=>$job_position_id,
                        "phone_number"=>$post["phone_number"],
                        "number"=>$post["recruit_num"],
                        "created_at"=>date("Y-m-d H:i:s",time())
                    ];
                    Db::connect('tmp')->table('job_position_contact_phone')->insert($data_phone);
                }elseif($post["contact_type"] == 2){
                    $data_phone = [
                        "job_position_id"=>$job_position_id,
                        "qq_number"=>$post["qq_number"],
                        "qq_qr_code"=>$post["qq_qr_code"],
                        "number"=>$post["qqrecruit_num"],
                        "created_at"=>date("Y-m-d H:i:s",time())
                    ];
                    Db::connect('tmp')->table('job_position_contact_qq')->insert($data_phone);

                }elseif($post["contact_type"] == 3){
                    $data_phone = [
                        "job_position_id"=>$job_position_id,
                        "wechat_number"=>$post["wechat_number"],
                        "wechat_qr_code"=>$post["wechat_qr_code"],
                        "number"=>$post["wxrecruit_num"],
                        "created_at"=>date("Y-m-d H:i:s",time())
                    ];
                    Db::connect('tmp')->table('job_position_contact_wechat')->insert($data_phone);
                }else{
                    $this->error("类型不存在");
                }
                if(empty($company_id)){
                    //添加分类
                    foreach($post["category_id"] as $k=>$v){
                        $data_cate = [
                            "job_position_id"=>$job_position_id,
                            "category_id"=>$v,
                            "created_at"=>date("Y-m-d H:i:s",time())
                        ];
                        Db::connect('tmp')->table('job_position_category')->insert($data_cate);
                    }
                }
               

            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $this->success('保存成功');
        }else{
            $company = Db::connect('tmp')->table('company')->order("id desc")->select();
            $this->assign("company",$company);
            $province = Db::connect('tmp')->table('area')->where('level', 0)->select()->toArray();
            $this->assign("province",$province);

            $category = Db::connect('tmp')->table('category')->order("id desc")->select()->toArray();
            $this->assign("category",$category);
            $this->assign("company_id",$company_id);
            return $this->fetch();
        }
      
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $admintype = session('admin.type');
        $company_id = session('admin.company_id');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            if($post["contact_type"] == 1){
                if(empty($post["phone_number"])){
                    $this->error("手机号不能为空");
                }
                if(empty($post["recruit_num"])){
                    $this->error("最大数量不能为空");
                }
            }elseif($post["contact_type"] == 2){
                if(empty($post["qq_number"]) && empty($post["qq_qr_code"])){
                    $this->error("QQ号和QQ二维码不能同时为空");
                }
                if(empty($post["qqrecruit_num"])){
                    $this->error("最大数量不能为空");
                }
            }elseif($post["contact_type"] == 3){
                if(empty($post["wechat_number"]) && empty($post["wechat_qr_code"])){
                    $this->error("微信号和微信二维码不能同时为空");
                }
                if(empty($post["recruit_num"])){
                    $this->error("最大数量不能为空");
                }
            }else{
                $this->error("类型不存在");
            }
            if(empty($company_id)){
                if(empty($post["category_id"])){
                    $this->error("分类不能为空");
                }
                if(empty($post["tags"])){
                    $this->error("职位标签不能为空");
                }
                $post["tags"] = explode(",",$post["tags"]);
                $tags = json_encode($post["tags"]);
            }
            if($post["isit_nationwide"] == 1){
                if(empty($post["region_province"])){
                    $this->error("省不能为空");
                }
                if(empty($post["region_city"])){
                    $this->error("市不能为空");
                }
                if(empty($post["region_region"])){
                    $this->error("区不能为空");
                }
            }
            if(empty($post["selection_date_start"])){
                $selection_date_start = NULL;
           }
            if(empty($post["selection_date_end"])){
                $selection_date_end = NULL;
            }
            if(empty($post["work_time_start"])){
                $work_time_start = NULL;
            }
            if(empty($post["work_time_end"])){
                $work_time_end = NULL;
            }
            if(empty($post["expiration_date"])){
                $expiration_date = NULL;
            }
            if($admintype == 1){
                $data = [
                    "company_id"=>$post["company_id"],
                    "position_name"=>$post["position_name"],
                    "salary"=>$post["salary"],
                    "salary_type"=>$post["salary_type"],
                    "region_province"=>$post["region_province"],
                    "region_city"=>$post["region_city"],
                    "region_region"=>$post["region_region"],
                    "job_description"=>$post["job_description"],
                    "selection_date_start"=>$selection_date_start,
                    "selection_date_end"=>$selection_date_end,
                    "work_time_start"=>$work_time_start,
                    "work_time_end"=>$work_time_end,
                    "greeting_message"=>$post["greeting_message"],
                    "contact_type"=>$post["contact_type"],
                    "gender_requirement"=>$post["gender_requirement"],
                    "work_experience"=>$post["work_experience"],
                    "min_age"=>$post["min_age"],
                    "max_age"=>$post["max_age"],
                    "experience_requirement"=>$post["experience_requirement"],
                    "cur_type"=>$post["cur_type"],
                    "expiration_date"=>$expiration_date,
                    "expected_num"=>$post["expected_num"], 
                    "tags"=>$tags,
                    "label"=>$post["label"],
                    "title_tag"=>$post["title_tag"],
                    "updated_at"=>date("Y-m-d H:i:s",time()),
                    "cur_status"=>$post["cur_status"]
                ];
            }else{
                $data = [
                    "company_id"=>$company_id,
                    "position_name"=>$post["position_name"],
                    "salary"=>$post["salary"],
                    "salary_type"=>$post["salary_type"],
                    "region_province"=>$post["region_province"],
                    "region_city"=>$post["region_city"],
                    "region_region"=>$post["region_region"],
                    "job_description"=>$post["job_description"],
                    "selection_date_start"=>$selection_date_start,
                    "selection_date_end"=>$selection_date_end,
                    "work_time_start"=>$work_time_start,
                    "work_time_end"=>$work_time_end,
                    "contact_message"=>$post["contact_message"],
                    "greeting_message"=>$post["greeting_message"],
                    "contact_type"=>$post["contact_type"],
                    "updated_at"=>date("Y-m-d H:i:s",time()),
                 
                ];
            }
            
 
            try {
                Db::connect('tmp')->table('job_position')->where("id",$id)->update($data);
                if(empty($company_id)){
                    $id = $id;
                }else{
                    $id = $company_id;
                }
                //判断联系类型 1手机 2QQ 3微信
                if($post["contact_type"] == 1){
                    $data_phone = [
                        "phone_number"=>$post["phone_number"],
                        "number"=>$post["recruit_num"],
                        "updated_at"=>date("Y-m-d H:i:s",time())
                    ];
                    Db::connect('tmp')->table('job_position_contact_phone')->where("job_position_id",$id)->update($data_phone);
                }elseif($post["contact_type"] == 2){
                    $data_phone = [
                        "qq_number"=>$post["qq_number"],
                        "qq_qr_code"=>$post["qq_qr_code"],
                        "number"=>$post["qqrecruit_num"],
                        "updated_at"=>date("Y-m-d H:i:s",time())
                    ];
                    Db::connect('tmp')->table('job_position_contact_qq')->where("job_position_id",$id)->update($data_phone);

                }elseif($post["contact_type"] == 3){
                    $data_phone = [
                        "wechat_number"=>$post["wechat_number"],
                        "wechat_qr_code"=>$post["wechat_qr_code"],
                        "number"=>$post["wxrecruit_num"],
                        "updated_at"=>date("Y-m-d H:i:s",time())
                    ];
                    Db::connect('tmp')->table('job_position_contact_wechat')->where("job_position_id",$id)->update($data_phone);
                }else{
                    $this->error("类型不存在");
                }
             
               
                if(empty($company_id)){
                    Db::connect('tmp')->table('job_position_category')->where("job_position_id",$id)->delete();
                    //添加分类
                    foreach($post["category_id"] as $k=>$v){
                        $data_cate = [
                            "job_position_id"=>$id,
                            "category_id"=>$v,
                            "created_at"=>date("Y-m-d H:i:s",time())
                        ];
                        Db::connect('tmp')->table('job_position_category')->insert($data_cate);
                    }
                }
            } catch (\Exception $e) {
                $this->error('保存失败',$e->getMessage());
            }
            $this->success('保存成功');
        }else{
            $row = Db::connect('tmp')->table('job_position')->where("id",$id)->find();
            $company = Db::connect('tmp')->table('company')->where("company_name",'<>','')->order("id desc")->field("id,company_name")->select()->toArray();
        
            $this->assign("company",$company);
            $province = Db::connect('tmp')->table('area')->where('level', 0)->select()->toArray();
            $this->assign("province",$province);

            $category = Db::connect('tmp')->table('category')->order("id desc")->select()->toArray();
            $this->assign("category",$category);
            if(!empty($row["region_city"])){
                $region_city = Db::connect('tmp')->table('area')->where('code', $row["region_city"])->find();
                if(!empty($region_city)){
                    $city = Db::connect('tmp')->table('area')->where('id', $region_city["id"])->select()->toArray();
                }else{
                    $city = [];
                }
                $this->assign("city",$city);
            }else{
                $this->assign("city",[]);
            }
            if(!empty($row["region_region"])){
                $region_region = Db::connect('tmp')->table('area')->where('code', $row["region_region"])->find();
                if(!empty($region_region)){
                    $region = Db::connect('tmp')->table('area')->where('id', $region_region["id"])->select()->toArray();
                }else{
                    $region = [];
                }
                $this->assign("region",$region);
            }else{
                $this->assign("region",[]);
            }
            
            $job_position_category = Db::connect('tmp')->table('job_position_category')->where("job_position_id",$id)->select()->toArray();
            $categoryid = array_column($job_position_category,"category_id");
            if(!empty($row["tags"])){
                $tags = json_decode($row["tags"]);
                $row["tags"] = implode(",",$tags);
            }
           

            //contact_type  联系类型 1手机 2qq 3微信
            if($row["contact_type"] == 1){
                $job_position_contact_phone = Db::connect('tmp')->table('job_position_contact_phone')->where("job_position_id",$id)->find();
                $this->assign('job_position_contact_phone', $job_position_contact_phone);
            }elseif($row["contact_type"] == 2){
                $job_position_contact_qq = Db::connect('tmp')->table('job_position_contact_qq')->where("job_position_id",$id)->find();
                $this->assign('job_position_contact_qq', $job_position_contact_qq);
            }elseif($row["contact_type"] == 3){
                $job_position_contact_wechat = Db::connect('tmp')->table('job_position_contact_wechat')->where("job_position_id",$id)->find();
                $this->assign('job_position_contact_wechat', $job_position_contact_wechat);
            }
            $this->assign('admintype', $admintype);
            $this->assign('company_id', $company_id);
            $this->assign('categoryid', $categoryid);
            $this->assign('row', $row);
            return $this->fetch();
        }
   
    }

    public function province(){
        if ($this->request->isAjax()) {
         
            $brand_id = $this->request->param('id');
            $code = Db::connect('tmp')->table('area')->where('code', $brand_id)->find();
            $list = Db::connect('tmp')->table('area')->where('pid', $code["id"])->select()->toArray();
           // $list = Db::name("area")->where('pid', $brand_id)->select()->toArray();
            $opt = "<option value=''>--请选择--</option>";
            foreach ($list as $k => $val) {
                $opt .= "<option value='{$val['code']}'>{$val['name']}</option>";
            }
            if ($opt) {
                exit(json_encode(array('opt' => $opt)));
            } else {
                exit(json_encode(array('status' => 1)));
            }
        }
    }

    public function city(){
        if ($this->request->isAjax()) {
         
            $brand_id = $this->request->param('id');
            $code = Db::connect('tmp')->table('area')->where('code', $brand_id)->find();
            $list = Db::connect('tmp')->table('area')->where('pid', $code["id"])->select()->toArray();
            
          //  $list = Db::name("area")->where('pid', $brand_id)->select()->toArray();
            $opt = "<option value=''>--请选择--</option>";
            foreach ($list as $k => $val) {
                $opt .= "<option value='{$val['code']}'>{$val['name']}</option>";
            }
            if ($opt) {
                exit(json_encode(array('opt' => $opt)));
            } else {
                exit(json_encode(array('status' => 1)));
            }
        }
    }


     /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $row = Db::connect('tmp')->table('job_position')->where('id', $id)->find();
        if(empty($row)){
            $this->error('数据不存在');
        }
        try {
            $save = Db::connect('tmp')->table('job_position')->where('id', $id)->update(["deleted_at"=>date("Y-m-d H:i:s",time())]);
            //删除职位和分类的关联数据
          //  Db::connect('tmp')->table('job_position_category')->where('job_position_id', $id)->delete();

        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

 
    //职位推荐
    public function recommended($id)
    {
        if ($this->request->isAjax()) {
            $row = Db::connect('tmp')->table('job_position')->find($id);
            empty($row) && $this->error('数据不存在');
            if($row["is_recommended"] == 1){
                $this->error('职位已推荐');
            }
            Db::connect('tmp')->table('job_position')->where("id",$id)->update(["is_recommended"=>1]);
            //加入职位推荐表 
            $job_position_recommend = Db::connect('tmp')->table('job_position_recommend')->where("job_position_id",$id)->find();
            if(empty($job_position_recommend)){
                Db::connect('tmp')->table('job_position_recommend')->insert(["job_position_id"=>$id,"created_at"=>date("Y-m-d H:i:s",time())]);
            }
           
            $this->success('保存成功');
        }
      
    }

    
}